package com.github.gin.sns.service;

import com.alibaba.fastjson.JSON;
import com.github.gin.sns.util.XStreamUtil;
import com.github.gin.sns.vo.MsgType;
import com.github.gin.sns.vo.req.IBaseMessage;
import com.github.gin.sns.vo.req.IImageMessage;
import com.github.gin.sns.vo.req.ITextMessage;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by GinPonson on 2017/1/1.
 */
public class MessageDispatcherManager {

    static Logger logger = LoggerFactory.getLogger(MessageDispatcherManager.class);
    static Map<String,Object> map = new HashMap<>();

    static{
        map.put(MsgType.MESSAGE_TYPE_TEXT,new ITextMessage());
        map.put(MsgType.MESSAGE_TYPE_IMAGE,new IImageMessage());
    }

    public static String doDispatcher(InputStream inputStream) throws IOException {
        String xml = IOUtils.toString(inputStream,"UTF-8");

        IBaseMessage message = (IBaseMessage) XStreamUtil.getInstance().fromXML(xml,new IBaseMessage());

        return doDispatcher((IBaseMessage)XStreamUtil.getInstance().fromXML(xml,map.get(message.getMsgType())));
    }

    public static String doDispatcher(IBaseMessage message){
        switch (message.getMsgType()){
            case MsgType.MESSAGE_TYPE_TEXT:
                return TextMessageService.process((ITextMessage) message);
            case MsgType.MESSAGE_TYPE_IMAGE:
                return ImageMessageService.process((IImageMessage) message);
            case MsgType.REQ_MESSAGE_TYPE_LOCATION:
                break;
            case MsgType.REQ_MESSAGE_TYPE_LINK:
                break;
            case MsgType.MESSAGE_TYPE_VOICE:
                break;
            case MsgType.MESSAGE_TYPE_VEDIO:
                break;
            case MsgType.REQ_MESSAGE_TYPE_EVENT:
                break;
        }
        return "";
    }

}
